package com.light.gamecomponents.physics;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.light.gamecomponents.IGameComponents;
import com.light.gamecomponents.gravitylight.GravityLight;
import com.light.roller.Roller;


public abstract class PhysicsComponents implements IUpdateHandler, IGameComponents{
	public static final float BOX_EIGEN = 30.0123f;
	public static final float SAFE_EIGEN = 123.13212f;
	public static final float BigBallEigenValue = 30.12313212f;
	public static final float SmallBallEigenValue = 30.123132122f;
	
	protected String m_resourceName;
	protected TextureRegion m_textureRegion;

	protected Roller m_game;
	protected Sprite m_sprite;
	protected Body m_body;
	protected Vector2 m_initialPosition;
	protected Vector2 m_size;
	protected PhysicsConnector m_physicsConnector;
	protected Vector2 m_tempVelocity = new Vector2(0, 0);
	protected float m_tempAngleVelocity = 0;
	
	public Vector2 getBodyPosition() { return m_body.getPosition(); }
	public Vector2 getSpritePosition() 
	{ 
		return new Vector2(m_sprite.getX() + m_sprite.getBaseWidth() / 2, 
				           m_sprite.getY() + m_sprite.getBaseHeight() / 2); 
	}
	
	public PhysicsComponents(Roller game, String resourceName, Vector2 initialPosition, Vector2 size) {
		this.m_game = game;
		this.m_resourceName = resourceName;
		this.m_initialPosition = initialPosition;
		this.m_size = size;
		
	}
	
	public abstract void setPosition(int x, int y);
	
	public Sprite getSprite() { return m_sprite; }
	
	public static boolean somethingBad = false;
	
	public void stopBody() 
	{ 
		if (m_body.getType() != BodyType.StaticBody)
		{
			m_tempVelocity = m_body.getLinearVelocity();
			m_tempAngleVelocity = m_body.getAngularVelocity();
			Roller.debugger.showText(m_tempVelocity.x + "," + m_tempVelocity.y + m_tempAngleVelocity);
			Roller.player.removeFromContactList(m_body.getFixtureList().get(0));
			m_body.setType(BodyType.StaticBody);
			m_body.setLinearVelocity(new Vector2(0.0f, 0.0f));
			m_body.setAngularVelocity(0);
			getSprite().setColor(0.5f, 0.5f, 0.5f);
		}
	}
	public void startBody() 
	{ 
		if (m_body.getType() != BodyType.DynamicBody)
		{			
			m_body.setType(BodyType.DynamicBody);
			m_body.setAngularVelocity(m_tempAngleVelocity);
			m_body.setLinearVelocity(m_tempVelocity);
			somethingBad = true;
			m_body.setActive(false);
			m_body.setActive(true);
			
			getSprite().setColor(1.0f, 1.0f, 1.0f);			
		}
	}
	
	public Body getBody()
	{
		return m_body;
	}
	
	public boolean isInLight(GravityLight light)
	{
		Vector2 disVec = new Vector2(getSpritePosition().x - light.getSpritePosition().x, 
									 getSpritePosition().y - light.getSpritePosition().y);

		float dis = (float)Math.sqrt(disVec.x * disVec.x + disVec.y * disVec.y);
		
		if (dis <= 140)
			return true;
		else
			return false;
	}
}
